/*
 * 作者:熊猫
 * 日期:2017 - 3 - 20 9 - 21 - 10
 *
 */

package net.lainiao.morethread.Chapter7.demo9;

import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/20.
 */
public class Main {
    public static void main(String[] args) {
        MyPriorityTransferQueue<Event> buffer=new MyPriorityTransferQueue<>();
        Product producer=new Product(buffer);
        Thread[] proThreads=new Thread[10];
        for (int i = 0; i < proThreads.length; i++) {
            proThreads[i]=new Thread(producer);
            proThreads[i].start();
        }
        Consumer consumer=new Consumer(buffer);
        Thread consumerThread=new Thread(consumer);
        consumerThread.start();

        System.out.println("Main : Buffer Consumer count "+buffer.getWaitingConsumerCount());
        Event myEvent=new Event("Core Event",0);
        try {
            buffer.transfer(myEvent);
            System.out.println("Main : My Event has ben transfered ");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < proThreads.length; i++) {
            try {
                proThreads[i].join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("Main : Buffer : Consumer count:"+buffer.getWaitingConsumerCount());
        myEvent=new Event("Core Event 2",0);
        try {
            buffer.transfer(myEvent);
            consumerThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Main Over");
    }
}
